<template>
	<xn-form-container
		:title="formData.id ? '编辑围栏' : '增加围栏'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="围栏名称：" name="fence_name">
				<a-input v-model:value="formData.fence_name" placeholder="请输入围栏名称" allow-clear />
			</a-form-item>
			<a-form-item label="行政区名称：" name="region_id">
				<a-tree-select
					v-model:value="formData.region_id"
					show-search
					style="width: 100%"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="P请选择行政区"
					allow-clear
					:tree-data="treeData"
					:fieldNames="{ children: 'children', label: 'name', value: 'id' }"
					tree-node-filter-prop="name"
					@select="treeChange"
				>
				</a-tree-select>
			</a-form-item>
			<a-form-item label="地图纵深配置项：" name="zoom">
				<a-input-number
					style="width: 100%"
					v-model:value="formData.zoom"
					placeholder="请输入地图纵深配置项"
					allow-clear
				/>
			</a-form-item>
			<a-form-item label="热力图配置项：" name="height">
				<a-input-number
					style="width: 100%"
					v-model:value="formData.height"
					:defaultValue="800"
					placeholder="请输入热力图配置项"
					allow-clear
				/>
			</a-form-item>
			<a-form-item
				label="热力图精度："
				name="map_precision"
				tooltip="按照围栏面积配置 取值面积 Precision值 (20万m以下:9, 20万m-7km:8, 7km-230km:7, 230km-7300km:6, 7300km-24万km:5, 24万km-762万km:4)"
			>
				<a-input-number
					style="width: 100%"
					v-model:value="formData.map_precision"
					placeholder="请输入热力图精度"
					allow-clear
				/>
			</a-form-item>
			<a-form-item label="围栏中心点X坐标：" name="x_axis">
				<a-input-number
					style="width: 100%"
					v-model:value="formData.x_axis"
					placeholder="请输入围栏中心点X坐标 (示例:38.45)"
					allow-clear
				/>
				<template #help>
					<a href="https://lbs.amap.com/tools/picker" target="_blank">坐标拾取器</a>
				</template>
			</a-form-item>
			<a-form-item label="围栏中心点Y坐标：" name="y_axis">
				<a-input-number
					style="width: 100%"
					v-model:value="formData.y_axis"
					placeholder="请输入围栏中心点Y坐标 (示例:106.10)"
					allow-clear
				/>
			</a-form-item>
			<a-form-item label="是否有效：" name="valid">
				<a-select
					v-model:value="formData.valid"
					placeholder="请选择是否有效"
					:options="[
						{ label: '有效', value: true },
						{ label: '失效', value: false }
					]"
					allow-clear
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required, rules } from '@/utils/formRules'
	import smCrypto from '@/utils/smCrypto'
	import dayjs from 'dayjs'
	import api from './api'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	let index = 0
	// 打开抽屉
	const onOpen = (record, id) => {
		visible.value = true
		index = id
		formData.value = {
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
		} else {
			formData.value = {}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}
	// 图标选择器回调
	const iconCallBack = (value) => {
		if (value) {
			formRef.value.clearValidate('icon')
		}
		formData.value.icon = value
	}

	// 默认要校验的
	const formRules = {
		fence_name: [
			{
				required: true,
				message: '请输入围栏名称',
				trigger: 'blur'
			}
		],
		region_id: [
			{
				required: true,
				message: '请选择行政区',
				trigger: 'blur'
			}
		],
		zoom: [
			{
				required: true,
				message: '请输入地图纵深配置项',
				trigger: 'blur'
			}
		],
		height: [
			{
				required: true,
				message: '请输入热力图配置项',
				trigger: ['blur', 'change']
			}
		],
		map_precision: [
			{
				required: true,
				message: '请输入热力图精度',
				trigger: 'blur'
			}
		],
		x_axis: [
			{
				required: true,
				message: '请输入围栏中心点X坐标',
				trigger: 'blur'
			}
		],
		y_axis: [
			{
				required: true,
				message: '围栏中心点Y坐标',
				trigger: 'blur'
			}
		],
		valid: [
			{
				required: true,
				message: '请选择是否有效',
				trigger: 'blur'
			}
		]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				let name = ''
				regionOptions.value.some((item) => {
					console.log(item.value, formData.value.region_id)
					if (item.value === formData.value.region_id) {
						name = item.name
						return true
					}
					return false
				})
				api
					.submitForm(
						{
							...formData.value,
							id: formData.value.id || index,
							fence_id: formData.value.fence_id || 100000 + index,
							...(formData.value.id
								? {
										create_time: dayjs(formData.value.create_time).format('YYYY-MM-DD HH:mm:ss')
								  }
								: {
										create_time: dayjs().format('YYYY-MM-DD HH:mm:ss')
								  }),
							modified_time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
							name
						},
						formData.value.id
					)
					.then(() => {
						onClose()
						emit('successful')
					})
			})
			.catch(() => {})
	}

	const regionOptions = ref([])

	const treeData = ref([])
	const loadRegion = () => {
		api.getRegionTree().then((res) => {
			treeData.value = res
			// regionOptions.value = res.map((item) => {
			// 	return {
			// 		label: item.name,
			// 		value: item.id
			// 	}
			// })
		})
	}
	loadRegion()

	const treeChange = (val, node, extra) => {
		console.log(node)
		formData.value.zoom = node.zoom
		formData.value.map_precision = node.map_precision
		formData.value.x_axis = node.x_axis
		formData.value.y_axis = node.y_axis
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
